package com.wh.wisdomsite.xbox.pr.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wh.wisdomsite.xbox.pr.dao.UnitMapper;
import com.wh.wisdomsite.xbox.pr.entity.Unit;
import com.wh.wisdomsite.xbox.pr.service.IUnitService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 单位ServiceImpl
 *
 * @version V1.0
 * @Package: com.wh.wisdomsite.xbox.pr.service.impl
 * @author: 丁奕
 * @date: 2018-08-08 19:09
 * @Copyright 公司版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2016
 **/
@Service("unitService")
@Transactional(rollbackFor = {Exception.class,RuntimeException.class})
public class UnitServiceImpl extends ServiceImpl<UnitMapper,Unit> implements IUnitService {
    private static final Log logger = LogFactory.getLog(UnitServiceImpl.class);

    @Autowired
    private UnitMapper unitMapper;

    @Override
    public Map<String, Object> getMapById(Serializable id) {
        return unitMapper.getMapById(id);
    }

    @Override
    public Page<Map<String, Object>> getMapsPage(Page page, Wrapper<Unit> wrapper) {
        SqlHelper.fillWrapper(page, wrapper);
        page.setRecords(this.baseMapper.getMapsPage(page, wrapper));
        return page;
    }

    @Override
    public Map<String, List<Unit>> getUnitMapList(Integer companyId) {
        Map<String, List<Unit>> unitMapList = new HashMap<>();
        EntityWrapper<Unit> ewUnit = new EntityWrapper<>();
        ewUnit.setEntity(new Unit());
        if(companyId != null){
            ewUnit.eq("companyId" , companyId);
        }
        List<Unit> unitList = unitMapper.selectList(ewUnit);
        for(Unit unit : unitList){
            List<Unit> unitListToStore = unitMapList.get(unit.getUnitModelCD().getPropertyCode());
            if(unitListToStore == null){
                unitListToStore = new ArrayList<>();
            }
            unitListToStore.add(unit);
            unitMapList.put(unit.getUnitModelCD().getPropertyCode(), unitListToStore);
        }
        unitMapList.put("unitMapList" , unitList);
        return unitMapList;
    }

}
